<template>
 
    <el-card>
      <template #header>
        <div class="card-header">
          <span>软件开发信息维护</span>
        </div>
      </template>
      <div v-loading="formLoading">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="所属软件" :span="1">
            <el-select v-model="formData.baseInfoId" placeholder="请选择所属软件" style="width: 100%">
              <el-option
                v-for="item in baseInfoOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <div v-if="formErrors.baseInfoId" class="error-message">{{ formErrors.baseInfoId }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="Git地址" :span="1">
            <el-input v-model="formData.gitUrl" placeholder="请输入Git地址" />
            <div v-if="formErrors.gitUrl" class="error-message">{{ formErrors.gitUrl }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="下载地址" :span="1">
            <el-input v-model="formData.downloadUrl" placeholder="请输入下载地址" />
            <div v-if="formErrors.downloadUrl" class="error-message">{{ formErrors.downloadUrl }}</div>
          </el-descriptions-item>
          <el-descriptions-item label="端口" :span="1">
            <el-input v-model="formData.port" placeholder="请输入端口" />
          </el-descriptions-item>
        </el-descriptions>
        <div class="form-actions">
          <el-button @click="submitForm" type="primary" :disabled="formLoading">提 交</el-button>
          <el-button @click="goBack">返 回</el-button>
        </div>
      </div>
    </el-card> 
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useMessage } from '@/hooks/web/useMessage'
import { DevelopApi, DevelopVO } from '@/api/compo/compoDevelop'
import { BaseInfoApi, BaseInfoOptionVO } from '@/api/compo/compoBaseInfo'
import { convertToOptions } from '@/utils/compoDict'

/** 软件开发信息 新增表单 */
defineOptions({ name: 'DevelopCreateForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const router = useRouter() // 路由

const formLoading = ref(false) // 表单的加载中
const baseInfoOptions = ref<any[]>([]) // 所属软件下拉选项
const formData = ref({
  id: undefined,
  baseInfoId: undefined,
  gitUrl: undefined,
  downloadUrl: undefined,
  port: undefined
})

// 表单错误信息
const formErrors = reactive({
  baseInfoId: '',
  gitUrl: '',
  downloadUrl: ''
})

/** 获取所属软件下拉列表 */
const getBaseInfoOptions = async () => {
  try {
    const data = await BaseInfoApi.listAllNormal()
    baseInfoOptions.value = convertToOptions(data)
  } catch (error) {
    console.error('获取所属软件列表失败:', error)
  }
}

/** 表单验证 */
const validateForm = () => {
  let isValid = true
  // 重置错误信息
  formErrors.baseInfoId = ''
  formErrors.gitUrl = ''
  formErrors.downloadUrl = ''

  // 所属软件验证
  if (!formData.value.baseInfoId) {
    formErrors.baseInfoId = '所属软件不能为空'
    isValid = false
  }

  // Git地址验证
  if (!formData.value.gitUrl) {
    formErrors.gitUrl = 'Git地址不能为空'
    isValid = false
  }

  // 下载地址验证
  if (!formData.value.downloadUrl) {
    formErrors.downloadUrl = '下载地址不能为空'
    isValid = false
  }

  return isValid
}

/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!validateForm()) {
    message.error('请填写完整信息')
    return
  }
  
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DevelopVO
    await DevelopApi.createOrUpdateDevelop(data)
    message.success(t('common.createSuccess'))
    goBack()
  } catch (error) {
    console.error('提交失败:', error)
  } finally {
    formLoading.value = false
  }
}

/** 返回列表页 */
const goBack = () => {
  router.push('/compo/develop')
}

/** 初始化 */
onMounted(async () => {
  await getBaseInfoOptions()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.form-actions {
  margin-top: 20px;
  text-align: center;
}

.error-message {
  color: #f56c6c;
  font-size: 12px;
  margin-top: 4px;
}

:deep(.el-descriptions__body) {
  width: 100%;
}

:deep(.el-descriptions__label) {
  width: 120px;
}

:deep(.el-descriptions__content) {
  min-height: 40px;
}
</style> 